﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace FinancialDB
{
    public partial class home_Homepage_Panel : Form
    {
        int x = 10;
        int y = 7;
        XmlDocument xmldoc = new XmlDocument();

        public home_Homepage_Panel()
        {
            InitializeComponent();
        }

        private void home_Homepage_Panel_Load(object sender, EventArgs e)
        {
            ToolTip toolTip = new ToolTip();
            toolTip.ReshowDelay = 10;
            
            xmldoc.Load(@"../../Users/userdata.xml");
            XmlNodeList colList = xmldoc.SelectNodes("/root/homepage/column");
            foreach (XmlNode col in colList)
            {
                Label title = new Label();
                title.AutoSize = false;
                title.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                title.Location = new System.Drawing.Point(x, y);
                title.Size = new System.Drawing.Size(350, 12);
                title.Text = ((XmlElement)col).GetAttribute("name");
                title.TextAlign = ContentAlignment.MiddleCenter;
                this.Controls.Add(title);

                y = y + 18;

                XmlNodeList funcList = col.SelectNodes("function");
                int check = funcList.Count % 2;
                int result = funcList.Count / 2;
                if (check != 0)
                {
                    result++;
                }
                if (result != 0)
                {
                    TableLayoutPanel tableLayout = new TableLayoutPanel();
                    tableLayout.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
                    tableLayout.ColumnCount = 2;
                    tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                    tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                    tableLayout.Location = new System.Drawing.Point(x, y);
                    tableLayout.RowCount = result;
                    tableLayout.Name = title.Text;
                    //tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
                    tableLayout.Size = new System.Drawing.Size(350, 20 * result + result + 1);

                    foreach (XmlNode func in funcList)
                    {
                        Label textLabel = new Label();
                        textLabel.Cursor = System.Windows.Forms.Cursors.Hand;
                        textLabel.ForeColor = System.Drawing.Color.Blue;
                        textLabel.Location = new System.Drawing.Point(0, 0);
                        textLabel.Margin = new System.Windows.Forms.Padding(0);
                        textLabel.Padding = new System.Windows.Forms.Padding(3, 0, 0, 0);
                        textLabel.Size = new System.Drawing.Size(153, 20);
                        textLabel.Text = ((XmlElement)func).GetAttribute("name");
                        textLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                        textLabel.MouseLeave += new System.EventHandler(textLabel_MouseLeave);
                        textLabel.Click += new System.EventHandler(textLabel_Click);
                        textLabel.MouseEnter += new System.EventHandler(textLabel_MouseEnter);

                        Label picLabel = new Label();
                        picLabel.Cursor = System.Windows.Forms.Cursors.Hand;
                        picLabel.Font = new System.Drawing.Font("宋体", 15F);
                        picLabel.ForeColor = System.Drawing.Color.Transparent;
                        picLabel.Location = new System.Drawing.Point(153, 0);
                        picLabel.Margin = new System.Windows.Forms.Padding(0);
                        picLabel.Size = new System.Drawing.Size(20, 20);
                        picLabel.Text = "-";
                        picLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                        picLabel.MouseLeave += new System.EventHandler(picLabel_MouseLeave);
                        picLabel.Click += new System.EventHandler(picLabel_Click);
                        picLabel.MouseEnter += new System.EventHandler(picLabel_MouseEnter);
                        toolTip.SetToolTip(picLabel, "从我的主页中删除");

                        Panel panel = new Panel();
                        panel.Controls.Add(textLabel);
                        panel.Controls.Add(picLabel);
                        panel.Dock = System.Windows.Forms.DockStyle.Fill;
                        panel.Location = new System.Drawing.Point(1, 1);
                        panel.Margin = new System.Windows.Forms.Padding(0);
                        panel.Padding = new System.Windows.Forms.Padding(3, 0, 0, 0);
                        panel.Size = new System.Drawing.Size(173, 20);

                        tableLayout.Controls.Add(panel);
                    }

                    this.Controls.Add(tableLayout);

                    y = y + tableLayout.Size.Height + 6;
                }
                else
                {
                    TableLayoutPanel tableLayout = new TableLayoutPanel();
                    tableLayout.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
                    tableLayout.ColumnCount = 2;
                    tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                    tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                    tableLayout.Location = new System.Drawing.Point(x, y);
                    tableLayout.RowCount = 1;
                    //tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
                    tableLayout.Size = new System.Drawing.Size(350, 22);
                    this.Controls.Add(tableLayout);

                    y = y + 28;
                }
            }

            LinkLabel home = new LinkLabel();
            home.AutoSize = true;
            home.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline;
            home.Location = new System.Drawing.Point(292, y);
            home.Size = new System.Drawing.Size(53, 12);
            home.TabIndex = 22;
            home.TabStop = true;
            home.Text = "设为首页";
            home.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            home.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(home_LinkClicked);
            this.Controls.Add(home);
        }

        private void textLabel_MouseEnter(object sender, EventArgs e)
        {
            Label llabel = sender as Label;
            llabel.Parent.BackColor = System.Drawing.Color.FromArgb(255, 48, 48);
            llabel.ForeColor = Color.White;
        }

        private void textLabel_MouseLeave(object sender, EventArgs e)
        {
            Label llabel = sender as Label;
            llabel.Parent.BackColor = Color.Transparent;
            llabel.ForeColor = System.Drawing.Color.FromArgb(0, 0, 255);
        }

        private void picLabel_MouseEnter(object sender, EventArgs e)
        {
            Label llabel = sender as Label;
            llabel.Parent.BackColor = System.Drawing.Color.FromArgb(255, 48, 48);
            llabel.Parent.Controls[0].ForeColor = Color.White;
        }

        private void picLabel_MouseLeave(object sender, EventArgs e)
        {
            Label llabel = sender as Label;
            llabel.Parent.BackColor = Color.Transparent;
            llabel.Parent.Controls[0].ForeColor = Color.Blue;
        }

        private void textLabel_Click(object sender, EventArgs e)
        {
        }

        private void picLabel_Click(object sender, EventArgs e)
        {
            Label llabel = sender as Label;
            DialogResult result = MessageBox.Show("确定要删除功能【" + llabel.Parent.Controls[0].Text + "】吗？", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                xmldoc.Load(@"../../Users/userdata.xml");
                XmlNode xn = xmldoc.SelectSingleNode("/root/homepage/column[@name='" + llabel.Parent.Parent.Name + "']/function[@name='" + llabel.Parent.Controls[0].Text + "']");
                xn.ParentNode.RemoveChild(xn);
                xmldoc.Save(@"../../Users/userdata.xml");
            }
            //如何更新form？
        }

        private void home_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            xmldoc.Load(@"../../Users/userdata.xml");
            XmlNode firstpage = xmldoc.SelectSingleNode("/root/firstpage");
            ((XmlElement)firstpage).SetAttribute("name", "homepage");
            xmldoc.Save(@"../../Users/userdata.xml");

            MessageBox.Show("已将当前页设置为默认首页。", "信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }
}
